package cc.rengu.igas.opms.core.service.trans.common;

import cc.rengu.igas.opms.common.enums.RespCodeEnum;
import cc.rengu.igas.opms.core.service.base.ManageService;
import cc.rengu.igas.opms.facade.request.GenUserAuthenticationTypeRequest;
import cc.rengu.igas.opms.facade.response.GenUserAuthenticationTypeResponse;
import cc.rengu.igas.share.common.constant.UserSessionConstant;
import cc.rengu.igas.share.common.enums.UserPayAuthTypeEnum;
import cc.rengu.igas.share.core.model.UserInfo;
import cc.rengu.igas.share.core.realize.UserService;
import cc.rengu.igas.share.core.realize.impl.UserServiceImpl;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.UserOrderInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.UserOrderInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.UserOrderInfo;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

/**
 * 当前交易认证方式查询
 * 00 免密
 * 01 密码验证
 * 02 密码+短信验证
 * 03 密码+软Ukey
 *
 * @date 2020/8/25.
 * create by fuyp 2020-09-10
 * modify by wangcheng 2021-09-13
 */
public class GenUserAuthenticationTypeService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        GenUserAuthenticationTypeRequest genUserAuthenticationTypeRequest = new GenUserAuthenticationTypeRequest();
        ConvertUtil.convertOutput(genUserAuthenticationTypeRequest);
        return genUserAuthenticationTypeRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {

        BizResponse<GenUserAuthenticationTypeResponse> bizResponse = new BizResponse();
        GenUserAuthenticationTypeResponse response = new GenUserAuthenticationTypeResponse();
        GenUserAuthenticationTypeRequest typeRequest = (GenUserAuthenticationTypeRequest) request;
        String instId = typeRequest.getHeader().getInstId();

        //请求参数合法性校验
        if (StringUtil.isEmptyOrNull(typeRequest.getBizScene()) || StringUtil.isEmptyOrNull(typeRequest.getUserId()) ||
                StringUtil.isEmptyOrNull(typeRequest.getOrderAmt()) || StringUtil.isEmptyOrNull(typeRequest.getUserOrderId())) {
            rglog.error("业务场景:<{}>或用户标识:<{}>或订单金额:<{}>或用户订单号:<{}>参数为空!", typeRequest.getBizScene(), typeRequest.getUserId(), typeRequest.getOrderAmt(), typeRequest.getUserOrderId());
            throw new BizException(RespCodeEnum.PARAM_ERROR.getRespCode(), RespCodeEnum.PARAM_ERROR.getRespDesc());
        }

        // 获取用户会话信息
        UserService userService = new UserServiceImpl();
        UserInfo userInfo = userService.getUserInfo(instId, typeRequest.getUserId(), typeRequest.getHeader().getSession());
        if (null == userInfo) {
            rglog.error("获取用户信息失败!");
            throw new BizException(RespCodeEnum.USER_NOT_LOGIN_OR_SESSION_IS_EXPIRED.getRespCode(), RespCodeEnum.USER_NOT_LOGIN_OR_SESSION_IS_EXPIRED.getRespDesc());
        }

        //校验订单
        UserOrderInfoMapper userOrderInfoMapper = new UserOrderInfoMapperImpl();
        UserOrderInfo userOrderInfo = userOrderInfoMapper.selectUserOrderInfoByUserOrderId(instId, typeRequest.getUserOrderId());
        if (userOrderInfo == null) {
            rglog.info("用户支付订单不存在");
            throw new BizException(RespCodeEnum.ORDER_NOEXIT.getRespCode(), RespCodeEnum.ORDER_NOEXIT.getRespDesc());
        }

        // 获取用户认证方式
        UserPayAuthTypeEnum userPayAuthTypeEnum = userService.getUserPayAuthType(instId, typeRequest.getUserId(), typeRequest.getOrderAmt(), typeRequest.getBizScene());
        String authType = userPayAuthTypeEnum.getType();
        // 主扫或者收银台，没有免密模式
        if (UserPayAuthTypeEnum.NO_PASSWORD.getType().equals(authType)) {
            rglog.error("主扫或者收银台，没有免密模式<{}>", authType);
            throw new BizException(RespCodeEnum.AUTH_AMT_PARAM_SET_ERR.getRespCode(), RespCodeEnum.AUTH_AMT_PARAM_SET_ERR.getRespDesc());
        }

        // 将用户支付认证方式保存到用户会话中
        userService.setUserSessionValue(UserSessionConstant.CHECK_PASSOWRD_FLAG, authType);
        response.setAuthType(authType);

        //设置响应结果
        bizResponse.setRspSysId(AppParamConstant.IGAS);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(response);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        rglog.debug("开始转换响应信息");
        BizResponse bizResponse = (BizResponse) response;
        GenUserAuthenticationTypeResponse typeResponse = (GenUserAuthenticationTypeResponse) bizResponse.getResult();
        ConvertUtil.convertInput(typeResponse);
    }
}
